package cn.vc.content.service.article;

import cn.vc.content.entity.article.ArticleHistory;
import cn.vc.content.mapper.article.ArticleHistoryMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleHistoryService {
    @Resource
    private ArticleHistoryMapper articleHistoryMapper;


    /**
     * 添加文章记录
     * @param userId
     * @param articleId
     */
    public void records(Long userId, Long articleId) {
        ArticleHistory articleHistory = this.selectMyArticleHistory(userId);
        if (articleHistory==null) {
            articleHistory = new ArticleHistory();
            articleHistory.setUserId(userId);
            // 创建历史记录
            List<Long> videoIdList = new ArrayList<>();
            videoIdList.add(articleId);
            // 添加历史记录
            articleHistory.setArticleIdList(videoIdList);
            // 添加数量
            articleHistory.setArticleNum(1L);
            // 添加
            articleHistoryMapper.insert(articleHistory);
        } else {
            // 添加记录
            articleHistory.getArticleIdList().add(articleId);
            // 更新记录
            articleHistory.setArticleIdList(articleHistory.getArticleIdList());
            articleHistory.setArticleNum(articleHistory.getArticleNum()+1);
//            System.out.println("文章记录 -> "+articleHistory.getId());
            // 添加
            articleHistoryMapper.updateById(articleHistory);
        }
    }


    /**
     * 查询文章历史记录
     */
    public ArticleHistory selectMyArticleHistory(Long userId) {
        LambdaQueryWrapper<ArticleHistory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ArticleHistory::getUserId, userId);
        return articleHistoryMapper.selectOne(lqw);
    }
}
